﻿
@{
    ViewBag.Title = "User";
    Layout = "~/Views/Shared/_MyLayoutPage.cshtml";
}



<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户管理</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入姓名" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAddUser"><i class="layui-icon">&#xe61f;</i>新增</button>

            @*<button type="button" class="layui-btn layui-btn-normal" onclick="apply()"><i class="layui-icon">&#xe63b;</i></button>*@
        </div>
    </div>
</form>

<table id="demo" lay-filter="UserTableList"></table>

<div id="addLayer" style="display:none;">
    <form class="layui-form" id="AddFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="UserName" lay-verify="required" class="layui-input">
                </div>
                <label class="layui-form-label">用户类型</label>
                <div class="layui-input-inline">
                    <select name="islinkman" id="SelRole" required lay-verify="required">
                        <option value="2">普通用户</option>
                        <option value="1">管理员</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"></label>
                <button type="button" class="layui-btn" id="BtnSave">保存</button>
            </div>
        </div>

        <button type="submit" id="formAddsubmit" class="layui-btn" lay-submit lay-filter="formAddVerify" style="display:none;">立即提交</button>
    </form>
</div>




<script type="text/javascript">
    var table;
    layui.use(['form', 'table', 'upload', 'layer'], function () {
        table = layui.table;
        var tab = parent.tab;
        var form = layui.form;
        var layer = layui.layer;
        var IsValidate = 0;
        var index;


        //拦截form提交消息
        form.on('submit(formAddVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        $("#BtnAddUser").on("click", function () {
            index = layer.open({
                type: 1,
                title: ['新增', 'font-size:18px;'],
                area: ['800px', '200px'],
                content: $("#addLayer") //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        $("#BtnSave").on("click", function () {
            $("#formAddsubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
            } else {
                return;
            }
            //return;
            var data = {};
            data["UserName"] = $("#UserName").val();
            data["role"] = $("#SelRole").val();
            $.ajax({
                type: "POST",
                url: "/Api/UserAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('新增成功', { icon: 6, time: 1000 });
                        layer.close(index);
                        table.reload("demo");

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/api/UserTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "id", Order: "desc", Condition: "and IsDelete=0" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                { field: 'UserName', title: '用户名', width: 150 }
                , {
                    field: 'role', title: '类型', width: 150,
                    templet: function (d) {
                        if (d.role == "1") {
                            return "管理员";
                        }
                        else if (d.role == "2") {
                            return "一般用户";
                        }
                        else {
                            return "";
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 150, fixed: "right",
                    templet: function (d) {
                        return '' +
                            '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });



        table.on("tool(UserTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除该用户？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                    , btn1: function (index) {

                        DataObj = {};
                        DataObj["Id"] = data.Id;

                        $.ajax({
                            type: "POST",
                            url: "/Api/UserDel",
                            async: false,
                            contentType: "application/json",
                            dataType: "json",
                            data: JSON.stringify(DataObj),
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });

    });


    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "Id", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "Id", Order: "Desc", Condition: " and IsDelete<>1 and UserName like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }





</script>
